package com.global.gbinventory.main.controller;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JInternalFrame;

import com.global.gbinventory.barang.controller.BarangController;
import com.global.gbinventory.barang.controller.BarangListController;
import com.global.gbinventory.barang.controller.GrupController;
import com.global.gbinventory.barang.controller.GudangController;
import com.global.gbinventory.barang.controller.MerekController;
import com.global.gbinventory.barang.controller.SatuanController;
import com.global.gbinventory.barang.util.DBMaster;
import com.global.gbinventory.data.controller.KaryawanController;
import com.global.gbinventory.data.controller.KaryawanListController;
import com.global.gbinventory.data.controller.PelangganController;
import com.global.gbinventory.data.controller.PelangganListController;
import com.global.gbinventory.data.controller.SupplierController;
import com.global.gbinventory.data.controller.SupplierListController;
import com.global.gbinventory.main.view.MainFrame;


public class MainController {
	
	private MainFrame window;
	private MerekController merekController;
	private SatuanController satuanController;
	private GudangController gudangController;
	private GrupController grupController;
	private BarangController barangController;
	
	private KaryawanController karyawanController;
	private PelangganController pelangganController;
	private SupplierController supplierController;
	
	private BarangListController barangListController;
	private KaryawanListController karyawanListController;
	private PelangganListController pelangganListController;
	private SupplierListController supplierListController;
	
	private DBMaster dbMaster;
	
	public MainController(MainFrame window){
		this.window = window;
		this.dbMaster = new DBMaster();
	}
	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					
					MainController controller = new MainController(new MainFrame());
					controller.start();
					
					
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	private void start(){
		window.displayFrame();
		addListeners();
	}
	
	private void addListeners(){
		window.addItemSetupMerekListener(new ItemSetupMerekListener());
		window.addItemSetupSatuanListener(new ItemSetupSatuanListener());
		window.addItemSetupGudangListener(new ItemSetupGudangListener());
		window.addItemSetupGrupListener(new ItemSetupGrupListener());
		window.addItemSetupBarangListener(new ItemSetupBarangListener());
		window.addItemSetupKaryawanListener(new ItemSetupKaryawanListener());
		window.addItemSetupPelangganListener(new ItemSetupPelangganListener());
		window.addItemSetupSupplierListener(new ItemSetupSupplierListener());
		
		window.addItemDaftarBarangListener(new ItemDaftarBarangListener());
		window.addItemDaftarKaryawanListener(new ItemDaftarKaryawanListener());
		window.addItemDaftarPelangganListener(new ItemDaftarPelangganListener());
		window.addItemDaftarSupplierListener(new ItemDaftarSupplierListener());
	}
	
	public void addFrameToDesktopPane(JInternalFrame frame){
		window.addFrameToDesktopPane(frame);
	}
	
	public void removeFrameFromDesktopPane(JInternalFrame frame){
		window.removeFrameFromDesktopPane(frame);
	}
	
	public DBMaster getDBMaster(){
		return this.dbMaster;
	}
	
	public MerekController getMerekController(){
		if(merekController == null){
			merekController = new MerekController(this);
		}
		return merekController;
	}
	
	public SatuanController getSatuanController(){
		if(satuanController == null){
			satuanController = new SatuanController(this);
		}
		return satuanController;
	}
	
	public GudangController getGudangController(){
		if(gudangController == null){
			gudangController = new GudangController(this);
		}
		return gudangController;
	}
	
	public GrupController getGrupController(){
		if(grupController == null){
			grupController = new GrupController(this);
		}
		return grupController;
	}
	
	public BarangController getBarangController(){
		if(barangController == null){
			barangController = new BarangController(this);
		}
		return barangController;
	}
	
	public BarangListController getBarangListController(){
		if(barangListController == null){
			barangListController = new BarangListController(this);
		}
		return barangListController;
	}
	
	public KaryawanController getKaryawanController(){
		if(karyawanController == null){
			karyawanController = new KaryawanController(this);
		}
		return karyawanController;
	}
	
	public KaryawanListController getKaryawanListController(){
		if(karyawanListController == null){
			karyawanListController = new KaryawanListController(this);
		}
		return karyawanListController;
	}
	
	public PelangganController getPelangganController(){
		if(pelangganController == null){
			pelangganController = new PelangganController(this);
		}
		return pelangganController;
	}
	
	public PelangganListController getPelangganListController(){
		if(pelangganListController == null){
			pelangganListController = new PelangganListController(this);
		}
		return pelangganListController;
	}
	
	public SupplierController getSupplierController(){
		if(supplierController == null){
			supplierController = new SupplierController(this);
		}
		return supplierController;
	}
	
	public SupplierListController getSupplierListController(){
		if(supplierListController == null){
			supplierListController = new SupplierListController(this);
		}
		return supplierListController;
	}
	
	class ItemSetupMerekListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			merekController = new MerekController(MainController.this);
			merekController.start(false);
		}
		
	}
	
	class ItemSetupSatuanListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			satuanController = new SatuanController(MainController.this);
			satuanController.start();
		}
		
	}
	
	class ItemSetupGudangListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			gudangController = new GudangController(MainController.this);
			gudangController.start();
		}
		
	}
	
	class ItemSetupGrupListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			getGrupController().start(false);
		}
		
	}
	
	class ItemSetupBarangListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			barangController = new BarangController(MainController.this);
			barangController.start();
		}
		
	}
	
	class ItemSetupKaryawanListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			karyawanController = new KaryawanController(MainController.this);
			karyawanController.start();
		}
		
	}
	
	class ItemSetupPelangganListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			pelangganController = new PelangganController(MainController.this);
			pelangganController.start();
		}
		
	}
	
	class ItemSetupSupplierListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			supplierController = new SupplierController(MainController.this);
			supplierController.start();
		}
		
	}
	
	class ItemDaftarBarangListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			
		}
		
	}
	
	class ItemDaftarKaryawanListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			getKaryawanListController().start(true);
		}
		
	}
	
	class ItemDaftarPelangganListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			getPelangganListController().start(true);
		}
		
	}
	
	class ItemDaftarSupplierListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			getSupplierListController().start(true);
		}
		
	}
	
}
